/*
 * XPreferenceTab.java
 * 
 * Copyright (c) 2006 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.dialog;

import java.util.prefs.Preferences;

import javax.swing.Icon;
import javax.swing.JComponent;

import org.xfc.editor.XEditor;
import org.xfc.model.XObject;

// TODO: preference tabs need 1) modified 2) valid 3) errors

// TODO: in the ctor, either take a JComponent for the contents or an XEditor -

/**
 * 
 * 
 * @author Devon Carew
 */
public abstract class XPreferenceTab
	extends XObject
{
	private JComponent 	componentContents;
	private XEditor		editorContents;
	
	/**
	 * 
	 */
	public XPreferenceTab()
	{
		
	}
	
	/**
	 * 
	 * 
	 * @param title
	 */
	public XPreferenceTab(String title)
	{
		this();
		
		setTitle(title);
	}
	
	/**
	 * 
	 * 
	 * @param title
	 * @param icon
	 */
	public XPreferenceTab(String title, Icon icon)
	{
		this();
		
		setTitle(title);
		setIcon(icon);
	}

	
	public String getTitle()
	{
		return (String)getValue("title");
	}
	
	public void setTitle(String title)
	{
		setValue("title", title);
	}
	
	
	public Icon getIcon()
	{
		return (Icon)getValue("icon");
	}
	
	public void setIcon(Icon icon)
	{
		setValue("icon", icon);
	}
	
	
	/**
	 * @return Returns the description.
	 */
	public String getDescription()
	{
		return (String)getValue("description");
	}
	
	/**
	 * @param description The description to set.
	 */
	public void setDescription(String description)
	{
		setValue("description", description);
	}
	
	/**
	 * Return the unique id for this tab. This is used to locate tab preferences. By default
	 * this returns the class name for this tab. Users can override this as needed.
	 * 
	 * @return the unique id for this tab
	 */
	public String getTabID()
	{
		return getClass().getName();
	}
	
	/**
	 * Returns the version number for this tab. This data is stored into the preferences when saving. On load,
	 * the saved version number is provided to the tab to allow it to handle migrating from older preference
	 * versions. 
	 * 
	 * @return the version number for this tab
	 */
	public double getTabVersion()
	{
		return 1.0d;
	}
	
	/**
	 * Load the data for this tab from the given preferences object.
	 * 
	 * @param preferencesVersion the version number for the saved preferences
	 * @param preferences the preferences to load
	 */
	public abstract void loadSettings(double preferencesVersion, Preferences preferences);
	
	/**
	 * Save the data for this tab into the given preferences object.
	 * 
	 * @param preferences
	 */
	public abstract void saveSettings(Preferences preferences);
	
	// TODO: doc
	public void setComponentContents(JComponent componentContents)
	{
		this.componentContents = componentContents;
	}
	
	// TODO: doc
	public void setEditorContents(XEditor editorContents)
	{
		// TODO: add listeners -
		
		this.editorContents = editorContents;
	}
	
	/**
	 * @return the JComponent to display to the user
	 */
	public JComponent getContent()
	{
		if (componentContents != null)
			return componentContents;
		
		if (editorContents != null)
			return editorContents;
		
		throw new IllegalStateException("getContent() is not overridden, and componentContents and editorContents are null");
	}
	
}
